package cn.xiaoliu.order.controller;

import cn.xiaoliu.basic.exception.BusinessException;
import cn.xiaoliu.basic.utils.AjaxResult;
import cn.xiaoliu.basic.utils.LoginContext;
import cn.xiaoliu.basic.utils.PageList;
import cn.xiaoliu.order.domain.AdoptOrder;
import cn.xiaoliu.order.domain.OrderAddress;
import cn.xiaoliu.order.query.AdoptOrderQuery;
import cn.xiaoliu.order.service.IAdoptOrderService;
import cn.xiaoliu.order.service.IProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){

        try{
            String payData = adoptOrderService.submit(params, LoginContext.getLogininfo(request));
            System.out.println(payData);
            return AjaxResult.me().setResultObj(payData);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下单失败！"+e.getMessage());
        }

    }
    //1.用户查询自己的订单
    @PostMapping("/user")
    public PageList<AdoptOrder> queryUserOrder(@RequestBody AdoptOrderQuery query,HttpServletRequest request){
        return adoptOrderService.queryUserOrder(query,LoginContext.getLogininfo(request));
    }
    //2.查询平台管理员订单或者店铺订单   根据shopid为空不为空判断是平台还是店铺
    @PostMapping("/admin")
    public PageList<AdoptOrder> queryAdmin(@RequestBody AdoptOrderQuery query,HttpServletRequest request){
        return adoptOrderService.queryAdmin(query,LoginContext.getLogininfo(request));
    }

    @GetMapping("{id}")
    public AdoptOrder queryById(@PathVariable("id") Long id){

        return adoptOrderService.queryById(id);
    }

    @GetMapping("/getOrderAdress/{unionPaySn}")
    public OrderAddress getOrderAdress(@PathVariable String unionPaySn){
        return adoptOrderService.getOrderAdress(unionPaySn);
    }
}
